﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GrainManage.Data.Util;
using GrainManage.Data.Pattern;
using GrainManage.Data.Model.Table;
using GrainManage.Data.Model;

namespace GrainManage.Data.Service
{
    public class DistrictService : BaseService
    {

        public DistrictService(GrainManage.Data.Access.AccessBase proc, IPattern pattern) : base(proc, pattern) { }

        public DistrictTable GetByID(int districtID)
        {
            DataSet ds = Proc.ExecuteQuery(ProcConst.District_GetAllByID, districtID);
            return ConvertToModel<DistrictTable>(GetFirstRow(ds));
        }

        public List<DistrictTable> GetByName(string name, IPageable page, ICountable records)
        {
            DataSet data = Proc.ExecuteQuery(ProcConst.District_GetAllByNameExtent, Pattern.SplitModel(name), page.PageIndex, page.PageSize);
            return ConvertToModel<DistrictTable>(data, records);
        }

        public string GetFatherName(int districtID)
        {
            var data = Proc.ExecuteScalar(ProcConst.District_GetFatherNameByID, districtID) as string;
            return data;
        }

        public List<string> GetChildName(int districtID, IPageable page)
        {
            var data = Proc.ExecuteQuery(ProcConst.District_GetChildNameByIDExtent, districtID, page.PageIndex, page.PageSize);
            var result = new List<string>(data.Tables[0].Rows.Count);
            foreach (DataRow row in data.Tables[0].Rows)
            {
                result.Add(Convert.ToString(row[0]));
            }
            return result;
        }

        public List<DistrictTable> GetDownwardDistrict(int districtID, int depth, IPageable page, ICountable records)
        {
            DataSet data = Proc.ExecuteQuery(ProcConst.District_GetDownwardByID, districtID, depth, page.PageIndex, page.PageSize);
            return ConvertToModel<DistrictTable>(data, records);
        }

        public List<DistrictTable> GetUpwardDistrict(int districtID, int depth, ICountable records)
        {
            DataSet data = Proc.ExecuteQuery(ProcConst.District_GetUpwardByID, districtID, depth);
            return ConvertToModel<DistrictTable>(data, records);
        }
    }
}
